package com.ruoyi.project.modules.tableIndex.controller;

import java.util.List;

import com.ruoyi.project.modules.table.domain.TableInfo;
import com.ruoyi.project.modules.table.service.ITableInfoService;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.project.modules.tableIndex.domain.TableIndex;
import com.ruoyi.project.modules.tableIndex.service.ITableIndexService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 索引信息Controller
 *
 * @author smallrain
 * @date 2022-07-09
 */
@Controller
@RequestMapping("/tableIndex/tableIndex")
public class TableIndexController extends BaseController {
    private String prefix = "tableIndex/tableIndex";

    @Autowired
    private ITableIndexService tableIndexService;

    @Autowired
    private ITableInfoService tableInfoService;

    //@RequiresPermissions("tableIndex:tableIndex:view")
    @GetMapping("/{tableId}")
    public String tableIndex(@PathVariable("tableId") Long tableId, ModelMap modelMap) {
        modelMap.put("tableId", tableId);
        return prefix + "/tableIndex";
    }

    /**
     * 查询索引信息列表
     */
    //@RequiresPermissions("tableIndex:tableIndex:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TableIndex tableIndex) {
        startPage();
        List<TableIndex> list = tableIndexService.selectTableIndexList(tableIndex);
        return getDataTable(list);
    }

    /**
     * 导出索引信息列表
     */
    @RequiresPermissions("tableIndex:tableIndex:export")
    @Log(title = "索引信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TableIndex tableIndex) {
        List<TableIndex> list = tableIndexService.selectTableIndexList(tableIndex);
        ExcelUtil<TableIndex> util = new ExcelUtil<TableIndex>(TableIndex.class);
        return util.exportExcel(list, "索引信息数据");
    }

    /**
     * 新增索引信息
     */
    @GetMapping("/add/{tableId}")
    public String add(@PathVariable("tableId") Long tableId, ModelMap modelMap) {
        modelMap.put("tableId", tableId);
        TableInfo tableInfo = tableInfoService.selectTableInfoById(tableId);
        modelMap.put("fieldInfoDatas", tableInfo.getFieldInfoList());
        return prefix + "/add";
    }

    /**
     * 新增保存索引信息
     */
    //@RequiresPermissions("tableIndex:tableIndex:add")
    @Log(title = "索引信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TableIndex tableIndex) {
        return toAjax(tableIndexService.insertTableIndex(tableIndex));
    }

    /**
     * 修改索引信息
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        TableIndex tableIndex = tableIndexService.selectTableIndexById(id);
        mmap.put("tableIndex", tableIndex);
        TableInfo tableInfo = tableInfoService.selectTableInfoById(tableIndex.getTableId());
        mmap.put("fieldInfoDatas", tableInfo.getFieldInfoList());
        return prefix + "/edit";
    }

    /**
     * 修改保存索引信息
     */
    //@RequiresPermissions("tableIndex:tableIndex:edit")
    @Log(title = "索引信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TableIndex tableIndex) {
        return toAjax(tableIndexService.updateTableIndex(tableIndex));
    }

    /**
     * 删除索引信息
     */
    //@RequiresPermissions("tableIndex:tableIndex:remove")
    @Log(title = "索引信息", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(tableIndexService.deleteTableIndexByIds(ids));
    }
}